// lib/src/nui_event.dart
class NuiRecognizeResult {
  final String result;
  final bool isLast;

  NuiRecognizeResult({required this.result, required this.isLast});

  factory NuiRecognizeResult.fromMap(Map<dynamic, dynamic> map) {
    return NuiRecognizeResult(
      result: map['result'] ?? '',
      isLast: (map['isLast'] ?? 1) == 1,
    );
  }
}

/// 阿里云NUI错误类型枚举
enum NuiErrorType {
  /// 默认客户端错误，对应了多个错误消息
  clientError(40000000, '默认的客户端错误，对应了多个错误消息', '用户使用了不合理的参数或者调用逻辑'),
  
  /// Token相关错误
  tokenExpiredOrInvalid(40000001, 'Token已过期或无效', '通用客户端错误，通常是涉及Token相关的不正确使用，例如Token过期或者非法'),
  
  /// 消息处理错误
  messageInvalid(40000002, '无效或者错误的报文消息', '无效或者错误的报文消息'),
  
  /// 参数错误
  parameterInvalid(40000003, '用户传递的参数有误', '用户传递的参数有误，一般常见于RESTful接口调用'),
  
  /// 请求过多
  tooManyRequests(40000005, '并发请求过多', '并发请求过多'),
  
  /// WAV头错误
  invalidWavHeader(40000009, '错误的消息头', '错误的消息头'),
  
  /// WAV头过大
  tooLargeWavHeader(40000009, '传输的语音WAV头不合法', '传输的语音WAV头不合法'),
  
  /// 未知错误
  unknown(-1, '未知错误', '未知错误');

  const NuiErrorType(this.code, this.message, this.description);

  final int code;
  final String message;
  final String description;

  /// 根据错误码获取错误类型
  static NuiErrorType fromCode(int code) {
    for (NuiErrorType type in NuiErrorType.values) {
      if (type.code == code) {
        return type;
      }
    }
    return NuiErrorType.unknown;
  }
}

class NuiError {
  final int errorCode;
  final String errorMessage;
  final NuiErrorType errorType;

  NuiError({
    required this.errorCode, 
    required this.errorMessage,
    NuiErrorType? errorType,
  }) : errorType = errorType ?? NuiErrorType.fromCode(errorCode);

  factory NuiError.fromMap(Map<dynamic, dynamic> map) {
    final code = map['errorCode'] ?? -1;
    final message = map['errorMessage'] ?? 'Unknown error';
    final type = NuiErrorType.fromCode(code);
    
    return NuiError(
      errorCode: code,
      errorMessage: message,
      errorType: type,
    );
  }

  /// 创建特定类型的错误
  factory NuiError.fromType(NuiErrorType type, {String? customMessage}) {
    return NuiError(
      errorCode: type.code,
      errorMessage: customMessage ?? type.message,
      errorType: type,
    );
  }

  /// 是否为Token相关错误
  bool get isTokenError => errorType == NuiErrorType.tokenExpiredOrInvalid;

  /// 是否为参数错误
  bool get isParameterError => errorType == NuiErrorType.parameterInvalid;

  /// 是否为网络相关错误
  bool get isNetworkError => errorType == NuiErrorType.tooManyRequests;

  /// 是否为音频格式错误
  bool get isAudioFormatError => 
      errorType == NuiErrorType.invalidWavHeader || 
      errorType == NuiErrorType.tooLargeWavHeader;

  /// 获取错误描述
  String get description => errorType.description;

  /// 获取用户友好的错误提示
  String get userFriendlyMessage {
    switch (errorType) {
      case NuiErrorType.tokenExpiredOrInvalid:
        return 'Token已过期，请重新获取';
      case NuiErrorType.parameterInvalid:
        return '参数配置错误，请检查配置';
      case NuiErrorType.tooManyRequests:
        return '请求过于频繁，请稍后重试';
      case NuiErrorType.invalidWavHeader:
      case NuiErrorType.tooLargeWavHeader:
        return '音频格式不正确，请检查音频设置';
      case NuiErrorType.messageInvalid:
        return '消息格式错误';
      case NuiErrorType.clientError:
        return '客户端错误，请检查调用参数';
      default:
        return errorMessage;
    }
  }

  @override
  String toString() {
    return 'NuiError(code: $errorCode, message: $errorMessage, type: ${errorType.name})';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is NuiError && 
           other.errorCode == errorCode && 
           other.errorMessage == errorMessage;
  }

  @override
  int get hashCode => errorCode.hashCode ^ errorMessage.hashCode;
}
